﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class MessageContactPagedList : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userid = Request["userid"] ?? "";
            int pagesize = 0;
            int pageindex = 0;
            int type = 0;
            try
            {
                pagesize = int.Parse(Request["pagesize"] ?? "0");
                pageindex = int.Parse(Request["pageindex"] ?? "0");
                type = int.Parse(Request["type"] ?? "0");
            }
            catch 
            {
                Response.Write("113");//无效的整型参数
            }

            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, userid, type))
            {
                Response.Write("112");//密钥不正确
                return;
            }

            if (Request["userid"] != null&&Request["pagesize"]!=null&&Request["pageindex"]!=null)
            {
                int recordcount = 0;
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }

                IList<Model.ContactList> list= BLL.ContactListBLL.Instance.GetPagedList(userid, type.ToString(),pagesize,pageindex,ref recordcount);

                Hashtable ht = new Hashtable();
                ht.Add("RecordCount", recordcount);
                ht.Add("ContactList", list);

                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string userid, int type)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + userid + (type * 11).ToString()).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;    
            }
        }
    }
}